﻿namespace eslib.FileManageLib
{
    /// <summary>
    /// 文件池
    /// </summary>
    public class FilePool
    {
        #region 对象存取

        /// <summary>
        /// 保存对象（使用默认目录）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <param name="t"></param>
        public void SaveObject<T>(string name, T t) where T : new()
        {
            SaveObject<T>(name, DefaultLongTimeDir, t);
        }

        /// <summary>
        /// 保存对象到临时目录
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <param name="t"></param>
        public void SaveTempObject<T>(string name, T t) where T : new()
        {
            SaveObject<T>(name, DefaultTempDir, t);
        }


        public void SaveObject<T>(string name, Dir dir, T t) where T : new()
        {
            FileItem item = new FileItem(name, dir);
            item.SaveObject<T>(t);
        }

        /// <summary>
        /// 加载对象（使用默认目录）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <returns></returns>
        public T LoadObject<T>(string name) where T : new()
        {
            return LoadObject<T>(name, DefaultLongTimeDir);
        }

        /// <summary>
        /// 加载对象(使用临时目录）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <returns></returns>
        public T LoadTempObject<T>(string name) where T : new()
        {
            return LoadObject<T>(name, DefaultTempDir);
        }


        public T LoadObject<T>(string name, Dir dir) where T : new()
        {
            FileItem item = new FileItem(name, dir);
            return item.LoadObject<T>();
        }

        #endregion


        public List<Dir> Dirs { get; } = new();


        public Dir? this[string dir]
            => Dirs.Where(d => d.Name == dir).FirstOrDefault();


        /// <summary>
        /// 默认储存目录
        /// </summary>
        public Dir DefaultLongTimeDir
            => Dirs.Where(d => d.Setting.GetDirType() == DirType.longtime).FirstOrDefault();

        /// <summary>
        /// 默认临时目录
        /// </summary>
        public Dir DefaultTempDir
            => Dirs.Where(d => d.Setting.GetDirType() == DirType.temp).FirstOrDefault();



        /// <summary>
        /// 文件池初始化，确定目录结构完整，并清理过期的临时文件
        /// </summary>
        public void Init()
        {
            cfg = FilePoolCfg.Instance;

            createDirs();
            clearValidateFiles();

            //初始化目录
            foreach (var dset in cfg.dirs)
            {
                Dir d = new Dir(dset.name);
                d.Setting = dset;
                d.LoadChildren();
                Dirs.Add(d);
            }
        }





        FilePoolCfg cfg;


        /// <summary>
        /// 清理过期文件
        /// </summary>
        private void clearValidateFiles()
        {
            foreach (var dir in cfg.dirs)
            {
                if (dir.GetDirType() == DirType.temp)
                {
                    DateTime valTime = DateTime.Now.AddDays(-dir.validate); //过期点

                    string[] files = Directory.GetFiles(dir.name);
                    foreach (string file in files)
                    {
                        FileInfo fi = new FileInfo(file);
                        //以创建时间判断过期
                        if (fi.CreationTime < valTime)
                        {
                            File.Delete(file);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 创建目录
        /// </summary>
        private void createDirs()
        {
            foreach (var dir in cfg.dirs)
            {
                DirTool.CreateDir(dir.name);
            }
        }
    }
}

